﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Discuz.Toolkit;
using Icst.Leaflike.Discuz;
using Icst.Leaflike.Logging;
using System.Configuration;
using System.Web;

namespace Icst.Leaflike.Discuz
{
    public static class UserAccount
    {
        private static readonly string COOKIE_DOMAIN = AppConfig.cookiedomain;

        public static bool Login(string userName, string password, out User user)
        {
            user = null;
            try
            {
                Logger.Info("TRY TO LOGIN with user [" + userName + "]");

                DiscuzSession ds = ConformityHandler.InitDiscuzSession();
                int uid = ds.GetUserID(ConformityHandler.CreateDiscuzNTUsername(userName));
                Logger.Info("DISCUZ USER ID CREATED: " + uid);
                Logger.Info("DO LOGIN with user [" + userName + "]");
                Logger.Info("CHECK USER INFO");
                user = ds.GetUserInfo(uid);
                if (user == null)
                {
                    Logger.Warning("FAILED TO FIND USER [" + userName + "]");
                    return false;
                }
                if (user.Password == password)
                {
                    ds.Login(uid, password, true, 100, COOKIE_DOMAIN);
                    Logger.Info("LOGIN SUCCEED with user [" + userName + "]");
                    return true;
                }
                return false;
            }
            catch(Exception e)
            {
                Logger.Warning("FAILED TO LOGIN with user [" + userName + "]");
                Logger.Info(e.Message);
                return false;
            }
        }

        public static bool IsLogon()
        {
            DiscuzSession ds = ConformityHandler.InitDiscuzSession();
            return !string.IsNullOrEmpty(ds.Cookie("userid", COOKIE_DOMAIN));
        }

        public static string UserName()
        {
            DiscuzSession ds = ConformityHandler.InitDiscuzSession();
            return ds.Cookie("username", COOKIE_DOMAIN);
        }

        public static long UserId()
        {
            DiscuzSession ds = ConformityHandler.InitDiscuzSession();
            return long.Parse(ds.Cookie("userid", COOKIE_DOMAIN));
        }

        public static void Logout()
        {
            try
            {
                Logger.Info("TRY TO LOGOUT");

                DiscuzSession ds = ConformityHandler.InitDiscuzSession();
                ds.Logout(COOKIE_DOMAIN);
            }
            catch (Exception e)
            {
                Logger.Warning("FAILED TO LOGOUT: " + e.Message);
            }
        }
        public static bool Register(string email, string userName, string password, out string error)
        {
            try
            {
                Logger.Info("REGISTER USER. EMAIL=" + email + ", USER NAME=" + userName);

                error = string.Empty;
                DiscuzSession ds = Discuz.ConformityHandler.InitDiscuzSession();
                //userName = Discuz.ConformityHandler.CreateDiscuzNTUsername(email);
                ds.Register(userName, password, email, true);

                Logger.Info("REGISTER SUCCEED. EMAIL=" + email + ", USER NAME=" + userName);

                return true;
            }
            catch (DiscuzException ex)
            {
                Logger.Info("REGISTER ERROR. EMAIL=" + email + ", USER NAME=" + userName + ", ERROR CODE=" + ex.ErrorCode +". "+ ex.Message);

                if (ex.ErrorCode == 109)
                {
                    error = "该Email地址已注册过帐户！请重试。";
                }
                else
                    error = "注册失败！请重试。";
                return false;
            }
        }
    }
}
